/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pgentity;

import config.PGConfig;
import db.DBContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import share.AMFBuilder;
import share.PGMacro;

/**
 *
 * @author Salm
 */
public class UrgentQuestContext {
    private final UrgentQuest uq;
    private List<UQPack> packs = null;

    private UrgentQuestContext(UrgentQuest uq) {
        this.uq = uq;
    }

    @Deprecated
    public static  UrgentQuestContext getUQ(String uid)
    {
        UrgentQuest uq = UrgentQuest.getUQ(uid);
        return new UrgentQuestContext(uq);
    }

    public void saveToDB()
    {
        uq.saveToDB();
        if (packs != null)
        {
            for (UQPack pack : packs) {
                pack.saveToDB();
            }
        }
    }

    public UrgentQuest getQuest() {
        return uq;
    }

    public synchronized List<UQPack> getPacks() {
        if (this.packs == null)
        {
            loadPacks();
        }
        
        return packs;
    }
    
    public Object buildAMF()
    {
        Map<String, Object> data = new HashMap();
        data.put(PGMacro.UQ_STATE, uq.getState().toString());
        
        switch (uq.getState())
        {
            case LOCKED:
                break;
            case COOLDOWN:
                data.put(PGMacro.UQ_NEXT_TIME, uq.getNext());
                data.put(PGMacro.UQ_PACKS, buildPacksActionAMF());
                break;
            case HAVE_QUEST:
                data.put(PGMacro.UQ_DEADLINE, uq.getDeadLine());
                data.put(PGMacro.UQ_TOTAL_PRIZE, uq.getTotalPrize());
                data.put(PGMacro.UQ_PACKS, buildPacksAMF());
                break;
        }
        
        return data;
    }
    
    private Object buildPacksAMF()
    {
        List<UQPack> _packs = this.getPacks();
        Map<String, Object> packAMFs = new HashMap(_packs.size());
        
        for (int i = 0; i < _packs.size(); ++i)
        {
            packAMFs.put(String.valueOf(i), _packs.get(i).buildAMF());
        }
        
        return packAMFs;
    }
    
    private Object buildPacksActionAMF()
    {
        List<UQPack> _packs = this.getPacks();
        Map<String, Object> packAMFs = new HashMap(_packs.size());
        
        for (int i = 0; i < _packs.size(); ++i)
        {
            packAMFs.put(String.valueOf(i), _packs.get(i).buildActionAMF());
        }
        
        return packAMFs;
    }
    
    private void loadPacks()
    {
        int nQuest = PGConfig.inst().uq().nQuest();
        this.packs = new ArrayList(nQuest);
        for (int i = 0; i < nQuest; ++i) {
            UQPack pack = UQPack.getUQPack(uq.getUid(), i);
            packs.add(pack);
        }
        
        packs = Collections.unmodifiableList(packs);
    }
}
